
import { _decorator, Component, Node, Vec2 } from 'cc';
const { ccclass, property } = _decorator;
// 对象名必须与文件一致
import { Common } from './common';

const camera_config = {
    max: 1.2,
    min: 1,
    idleTime: 3
}
@ccclass('Camera')
export class Camera extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;
	private _curCameraPos: Vec2 = new Vec2();
    @property({ type: Node })
    mainCamera = null;
    start() {
        // [3]

        //路的边界
        this.boundary = 6000 / 2;
        // 摄像机移动范围=路的边界减去屏幕的一半宽度
        this.cameraBoundary = this.boundary - (964.021 / 2)
        this.CameraTime = 0;
        this.onScale = false;
    }



    //更新摄像机位置
    updateCamera(dt) {
        let x = this.node.getPosition().x;
        let y = this.node.getPosition().y;
   
        if(this.node._State === Common.State.IDLE && !this.checkBoundary(x)){
            this.CameraTime += dt;
            if (this.CameraTime >= camera_config.idleTime && this.mainCamera.zoomRatio < camera_config.max) {
                this.mainCamera.zoomRatio += 0.002;
            }
        } else {
            this.CameraTime = 0;
            if (this.mainCamera.zoomRatio > camera_config.min) {
                this.mainCamera.zoomRatio -= 0.01;
            } else {
                this.mainCamera.zoomRatio = camera_config.min;
            }
        }
        //只要主角的x坐示少于等于边界或者大于等于边界，即不移动
        // if (this.checkBoundary(x)) return;
        //就这样，摄像机就会跟随着主角移动了
        //把摄像机的座标设为主角一样
        // console.log(x,y) 

        this.mainCamera.getPosition(this._curCameraPos);
        // console.log(this.node.position,x,this.node.position)
        this._curCameraPos.x =x;
        this.mainCamera.setPosition(this._curCameraPos);
    }

    checkBoundary(x) {
        return x <= -this.cameraBoundary || x >= this.cameraBoundary;
    }
    update(deltaTime: number) {
        // [4]
        this.updateCamera(deltaTime);
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
